package com.hdaccp.hadoop.mr;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.util.StringTokenizer;

/**
 * input
 */
public class WordCountApp {
    /**
     * LongWritable :首行首字母的偏移量
     * Text:每一行的数据
     * Text：输出的数据
     * LongWritable :次数
     */
    public static class WordCounMapper extends Mapper<LongWritable,Text,Text,LongWritable>
    {
        /**
         * 被分析的文件有多少行，就执行多少次
         * @param key
         * @param value
         * @param context
         * @throws IOException
         * @throws InterruptedException
         */
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            //
         //   StringTokenizer stringTokenizer = new StringTokenizer(value.toString()) ;
           /* while (stringTokenizer.hasMoreTokens())
            {
                 String str =  stringTokenizer.nextToken() ;

                 context.write(new Text(str),new LongWritable(1));
            }*/
         //  context.write(new Text(stringTokenizer.nextToken()),new LongWritable(1));


            System.out.println(key.get() + "-----" + value.toString());


        }
    }

    public static class WorldCountReduce extends Reducer<Text,LongWritable,Text,LongWritable>
    {
        @Override
        protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
            super.reduce(key, values, context);
        }
    }

    /**
     * 向集群提交作业
     * @param args
     */
    public static void main(String[] args) throws  Exception{
        if(args.length==0){
            System.err.print("请输入参数");
            System.exit(1);
        }

        //
        Configuration cfg = new Configuration() ;
        //创建一个作业
        Job job = Job.getInstance(cfg,"wordcount") ;

        



        //配置作业
        job.setJarByClass(WordCountApp.class);
        job.setMapperClass(WordCounMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);
        //读hdfs上的那个文件
        Path path = new Path(args[0]) ;
       // FileInputFormat.setInputPaths(job,args[0]);
        FileInputFormat.setInputPaths(job,path);


        FileOutputFormat.setOutputPath(job,new Path(args[1]));

        //提交作业到集群
        System.exit(job.waitForCompletion(true) ? 0 : 1);



    }
}
